﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）
//【头文件包含】
#include"Sort.h"
#include"Stack.h"

//【函数定义】
/*栈初始化*/void StackInit(Stk* ps)
{
	assert(ps);
	ps->data = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
/*栈空间销毁*/void StackDestroy(Stk* ps)
{
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->top = ps->capacity = 0;
}
/*栈插入*/void StackPush(Stk* ps, StkDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		StkDataType* tmp = (StkDataType*)realloc(ps->data, sizeof(StkDataType) * newCapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->data = tmp;
		ps->capacity = newCapacity;
	}
	ps->data[ps->top]=x;
	ps->top++;
}
/*栈删除*/void StackPop(Stk* ps)
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
/*栈顶数据提取*/StkDataType StackTop(Stk* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->data[ps->top - 1];
}
/*栈大小计算*/int StackSize(Stk* ps)
{
	assert(ps);
	return (ps->top);
}
/*栈空判断*/bool StackEmpty(Stk* ps)
{
	assert(ps);
	return ps->top == 0;
}
/*栈打印*/void StackPrint(Stk* ps)
{
	assert(ps);
	int i = 0;
	printf("BOTTOM|");
	while (i < ps->top)
	{
		printf("[%d] ", ps->data[i]);
		i++;
	}
	printf("|TOP\n");
}
/*栈从栈顶打印*/void StackPrintTop(Stk* ps)
{
	assert(ps);
	int i = ps->top;
	printf("TOP|"); 
	while (i > 0)
	{
		printf("[%d] ", ps->data[i-1]);
		i--;
	}

	printf("|BOTTOM\n");
}
